        TITL DAI FIRMWARE D23D-D3F3  V1.1
        ORG   :D23D
*
*
*
*  =============
*** I/O HANDLER ***
*  =============
*
*
*********************
* RUN basiccmd SAVE *
*********************
*
* Valid as direct command and in program.
* Clears the Heap, zeroes all variables, evaluate
* an evt. program name and writes a file of type
* 0 (BASIC) on tape.
*
* Exit: HL: Points to end symboltable.
*       DE: Length symboltable.
*       BC: Updated.
*
RSAVE   CALL  SCRATC    Empty HEAP + symtab
        LHLD  STBBGN    Get start symtab
        XCHG            in DE
        LHLD  STBUSE    End symtab in HL
        CALL  SUBDE     Calculate length symtab
        PUSH  H         Preserve length symtab
        LHLD  TXTBGN    Get start textbuf
        XCHG            in DE
        CALL  SUBDE     Calculate length textbuf
        PUSH  H         Preserve length textbuf
        PUSH  D         Preserve start textbuf
        CALL  REXSR     (0) Evaluate program name
*
************
* RUN SAVE *
************
*
* Instructions only moved.
*
        MVI   A,:30
        CALL  WOPEN
        POP   H
        POP   D
        CALL  WBLK
        POP   D
        JMP   MPT10
RSVEX   JNC   RLEAR     ] Evt run loading error
        JMP   RCLOSE    ] Stop cassette motors 
*
WNDWN   LHLD  ECURY     ] Get Y-offset cursor in
 REM document
        CALL  SUBDE2    ] HL=HL-DE
        DCR   L         ] -1
        STC             ] Set 'window changed' flag
        RET             ]
*
************
* RUN LOAD *
************
*
* Instructions only moved.
*
RLOAD   CALL  SCRATC
        CALL  REXSR
        PUSH  B
*
        MVI   B,:30     File type byte 0
        PUSH  H         Preserve length name reqd
        LHLD  CURRNT    Get CURRNT
        MOV   A,H
        ORA   L         Load during run program?
        MVI   C,:00
        JNZ   RLD10     If during run; C=00
        DCR   C         Else C=FF
RLD10   POP   H
        CALL  ROPEN     Switch on cassette motors;
 REM read header + name
        LHLD  SCRBOT    Get end free RAM
        XCHG            Max. RAM in DE
        LHLD  TXTBGN    Start textbuf in HL
        CALL  RBLK      Load textbuffer
        SHLD  TXTUSE    Store end textbuffer
        CC    RBLK      Load symboltable
        CALL  MPT11     Store end symtab; stop
 REM cassette motors.
        POP   B
        EI              Enable interrupts
        JNC   RLERR     If loading error
        MVI   A,:00     No loading error
        RET 
*
*********************
* RUN LOADING ERROR *
*********************
*
* The programbuffers are restored. A error message
* is printed.
*
RLERR   PUSH  PSW
        CALL  RNEW      Run 'NEW'
        POP   PSW
        LXI   H,:0000
        SHLD  CURRNT    Set CURRNT=0
RLEAR   ADI   :0B
        JMP   ERROR     Run 'LOADING ERROR ..'
*
*
******************
* OPEN TAPE FILE *
******************
*
* Entry: A:  File type.
*        HL: Points to file name.
* Exit:  HL: Points beyond file name.
*        DE: Length of name.
*        BC: Preserved.
*        A:  Checksum on name.
*
CWOPEN  PUSH  PSW
        CALL  MPT21     Init. write file leader
        POP   PSW
        CALL  WBYTE     Write file type byte
        JMP   MPT22     Get name length, write it
 REM on tape, incl. its c.s.
*
**********************
* RUN basiccmd CHECK *
**********************
*
* Valid as direct command only.
* Checks on file type and name. For all files
* with type <3, a checksum on all data is done.
* This routine remains in a endless loop and
* can be aborted with BREAK only.
*
*RCHECK
CHK10   LXI   H,:0000   No program name given
        LXI   B,:00FF   Any file type
*
*************
* RUN CHECK *
*************
*
* CALL :D7E6 is replaced by the contents of D7E6.
* No change in execution.
*
        CALL  ROPEN
        CPI   :33
        JNC   CHK30
        INR   C
        MVI   A,:00     ] Old contents
        CALL  MBLK      ] D7E6
        NOP 
        CZ    MBLK      Read + check next block
        JNZ   CHK20     If reading error
        CALL  PMSGR     Print 'OK', car.ret
        DBL   MSG17
        JMP   CHK10     Wait for next file
 REM
* If checksum error:
 REM
CHK20   CALL  PMSGR     Print 'BAD'
        DBL   MSG14
CHK30   CALL  CRLF      Print car.ret
        JMP   CHK10     Wait for next file
*
***********************
* WRITE BLOCK ON TAPE *
***********************
*
* Entry: HL: Startaddres block.
*        DE: Length block.
* Exit:  HL: 1st byte after block.
*        A: Checksum on block contents.
*        BCDE preserved.
*
CWBLK   PUSH  B
        PUSH  D
        NOP 
        CALL  LD37      Write block length +
 REM c.s. on length
        MVI   B,:56     Initial checksum value
LD34    MOV   A,D
        ORA   E
        JZ    LD35      If all bytes written
        DCX   D
        MOV   A,M       Get byte of block
        INX   H         Point to next byte
        CALL  LD36      Write byte, update checksum
        JMP   LD34      Next byte
 REM
* If all data written: write c.s. on block:
 REM
LD35    MOV   A,B       Get calculated checksum
        CALL  WBYTE     Write checksum
        NOP 
        POP   D
        POP   B
        RET 
*
*******************************
* WRITE BYTE, UPDATE CHECKSUM *
*******************************
*
* Entry: Byte to be written in A.
*        Checksum in B.
* Exit:  New checksum in B; A corrupted.
*        CDEHL preserved.
*
LD36    CALL  WBYTE     Write byte
        XRA   B
        RLC 
        MOV   B,A       Update checksum
        RET 
*
***************************************
* WRITE BLOCK LENGTH, UPDATE CHECKSUM *
***************************************
*
* Entry: DE: length block.
* Exit:  DEHL preserved.
*
LD37    MVI   B,:56     Init checksum
        MOV   A,D       Get highest length byte,
        CALL  LD36      write it, update c.s.
        MOV   A,E       Get lowest length byte,
        CALL  LD36      write it, update c.s
        MOV   A,B       Get checksum
        CALL  WBYTE     Write checksum on length
        RET 
 REM
**********************
* START FILE READING *
**********************
*
* Entry: HL: Address length byte of name requested.
*        B:  File type byte requested.
*        C:  00 when reading during run program,
*            else FF.
* Exit:  A:  File type byte.
*        HL: Points to 1st byte of name requested.
*        DE: Length name requested.
*        BC: preserved.
*
CROPEN  PUSH  PSW
        CALL  MPT23     Switch cassette motors on,
 REM init. registers
RPN10   NOP 
        NOP 
        NOP 
        NOP 
        NOP 
        POP   PSW
        CALL  RHDR      Read file header
        PUSH  PSW
        CALL  MPT24     Display file type byte
        SUB   B
        CALL  CMBLK     Read and check header,
 REM program name, file type byte
        ORA   A         0 if everything OK.
        JNZ   MPT54     If failure
        POP   PSW
        RET 
*
**************
* READ BLOCK *
**************
*
* Read length, contents and checksum of a block
*
* Entry: HL: Addr. where to dump data read.
*        DE: End free space.
* Exit:  CY=1: No error:
*           HL: Next free address.
*           BCDE preserved; AF corrupted.
*        CY=0: Loading error:
*           BCDEHL preserved.
*           A: Type of loading error.
*
CRBLK   PUSH  B
        PUSH  D
        PUSH  H
        CALL  MPT25     Calculate free RAM space
        XCHG            Free RAM in DE
        CALL  INLNG     Read block length + c.s.
 REM length in HL
        JC    LBK40     If loading error 3
        ORA   A
        MVI   A,:00     Loading error 0
        JNZ   LOERR     If checksum error 0
        PUSH  H         Save length block
        DAD   D         Calculate free RAM
        POP   D         Get length block
        INR   A         Loading error 1
        POP   H
        PUSH  H         Restore begin addr.
        JC    LOERR     If loading error 1
        MVI   B,:56     Init checksum
LBK10   MOV   A,D
        ORA   E
        JZ    LBK20     If whole block read
        DCX   D
        CALL  INSC      Read next byte, update c.s
        JC    LBK40     If loading error 3
        MOV   M,A       Store byte in buffer
        INX   H
        JMP   LBK10     Next byte
 REM
* If whole block read:
 REM
LBK20   CALL  RBYTE     Read checksum block contents
        JC    LBK40     If loading error 3
        CMP   B         Check checksum
        MVI   A,:02     Loading error 2
        JNZ   LOERR     If loading error 2
        JMP   MPT26     CY=1, return: no error
 REM
* If loading error:
 REM
LBK40   MVI   A,:03     Loading error 3
LOERR   ORA   A
        JMP   LBK30     Return with CY=0: error
*
*********************************
* READ BYTE, CALCULATE CHECKSUM *
*********************************
*
* Entry: B: Checksum.
* Exit:  A: Byte read.
*        B: Updated checksum.
*        CDEHL preserved.
*
INSC    CALL  RBYTE     Read byte
RBUEX   PUSH  PSW
        XRA   B         Calculate checksum
        RLC 
        MOV   B,A       Store new value
        POP   PSW
        RET 
*
********************
* READ NAME LENGTH *
********************
*
* Entry: No conditions.
* Exit:  HL: Length name read.
*        A:  Result checksum check (0 if OK).
*        BCDE: preserved.
*        CY=0: O.K.; CY=1: Out of data.
*
INLNG   PUSH  B
        MVI   B,:56     Init. checksum
        CALL  INSC      Read highest length byte
 REM and update checksum
        MOV   H,A
        CNC   INSC      Read lowest length byte
 REM and update checksum
        MOV   L,A
        CNC   RBYTE     Read checksum on length
RHLEX   PUSH  PSW
        SUB   B         Check checksum
        MOV   B,A
        POP   PSW
        MOV   A,B
        POP   B
        RET 
*
*******************************************
* READ + CHECK PROGRAM NAME AND FILE TYPE *
*******************************************
*
* Routine searches for proper file name by
* reading file name and compare it with name
* requested.
*
* Entry: A:  Evt. difference in file type byte
*            read and requested.
*        B:  Requested file type.
*        C:  00 during run program, else FF.
*        DE: Length requested.
*        HL: Address 1st byte name requested.
* Exit:  BCDEHL preserved.
*        A=0: All OK.
*        A=1: Loading error 1.
*
CMBLK   PUSH  B         Save file type + RUN flag
        PUSH  H         Save addr reqd name
        MOV   B,A       Store deviation file type
MBK10   PUSH  D         Save req. name length
        PUSH  H
        CALL  INLNG     Read + check program name
 REM evt. c.s.failure in A
        JC    MBK50     If reading error
        ORA   A
        JNZ   MBK50     If checksum error
        PUSH  H         Save length name on tape
        CALL  SUBDE     Calculate difference name
 REM lengths reqd and on tape
        MOV   A,H
        ORA   L
        MOV   H,A       Difference in H
        MOV   L,B       Difference file type in L
        POP   D         Get length name on tape
        MVI   B,:56     Initiate checksum
MBK20   XTHL            Get byte reqd name
        MOV   A,D
        ORA   E         Length name on tape = 0 ?
        JZ    MBK30     If length = 0, or whole
 REM name read.
        DCX   D
        CALL  INSC      Read bytes of name, update
 REM checksum
        JC    MBK50     If reading error
        DCR   C
        INR   C         Load during run?
        PUSH  PSW       Save length name on tape
        CNZ   FPRNT     Display program name!
        POP   PSW       Get byte of name on tape
        XRA   M         Compare with name reqd
        INX   H
        XTHL            Get 'difference flag'
        ORA   H         Update it
        MOV   H,A       and store it in H
        JMP   MBK20     Next byte
 REM
* If whole name read:
 REM
MBK30   CALL  RBYTE     Read c.s on name contents
        JC    MBK50     If reading error
MBEX    XRA   B         Check checksum
        POP   H
        ORA   L         Check file type
        MOV   L,A
        POP   D         Get length req. name
        MOV   A,D
        ORA   E         No name requested ?
        JZ    MBK40     If load without name
        MOV   A,H       Difference in names?
MBK40   ORA   L         Take also other checks in
 REM account
MBK45   POP   H
        POP   B
        RET 
 REM
* If error:
 REM
MBK50   POP   H
        POP   D
        MVI   A,:01     Loading error 1
        JMP   MBK40
*
*
*
        UNL
SCRATC  EQU   :CB23
STBBGN  EQU   :02A1
STBUSE  EQU   :02A3
SUBDE   EQU   :DE1A
TXTBGN  EQU   :029F
REXSR   EQU   :E791
WOPEN   EQU   :02C5
WBLK    EQU   :02C8
MPT10   EQU   :D7D8
RCLOSE  EQU   :02D4
ECURY   EQU   :00AC
SUBDE2  EQU   :E6F2
CURRNT  EQU   :0100
ROPEN   EQU   :02CE
SCRBOT  EQU   :02A5
RBLK    EQU   :02D1
TXTUSE  EQU   :02A1
MPT11   EQU   :D71A
RNEW    EQU   :DEB8
ERROR   EQU   :D9F5
MPT21   EQU   :D720
WBYTE   EQU   :D509
MPT22   EQU   :D7F8
MBLK    EQU   :02D7
PMSGR   EQU   :DAFF
MSG17   EQU   :DBC0
MSG14   EQU   :DBDB
CRLF    EQU   :DD5E
MPT23   EQU   :D7FF
RHDR    EQU   :D3F4
MPT24   EQU   :D78A
MPT54   EQU   :D783
MPT25   EQU   :D790
RBYTE   EQU   :D4D4
MPT26   EQU   :C6B4
LBK30   EQU   :C6B6
FPRNT   EQU   :D7EC
        LST
        END 
